<template>
  <div style="padding-left: 20px;text-align: left">
    <el-form
      ref="elForm"
      :model="formData"
      :rules="rules"
      size="small"
      label-width="100px"
      label-position="left"
      :inline="true"
      style="padding: 10px"
    >

      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="formData.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 150px"
          @keyup.enter.native="getPageData"
        />
      </el-form-item>

      <el-form-item>
        <BeeButton :loading="loading" code="loginUserSearch" type="primary" @click="getPageData">查询</BeeButton>
      </el-form-item>

    </el-form>

    <el-row>
      <el-col :span="24">
        <div v-if="tableData.length!==0">
          <el-table
            v-loading="loading"
            :size="isMobile()?'mini':'medium'"
            :data="tableData"
            style="width: 100%"
            border
            tooltip-effect="light"
          >
            <el-table-column
              prop="userName"
              label="用户名称"
              show-overflow-tooltip
            />
            <el-table-column
              prop="ip"
              label="登录ip"
            />
            <el-table-column
              prop="token"
              label="用户令牌"
              width="300px"
            >
              <template slot-scope="scope">
                <BeeLink
                  code="loginUserTokenInfo"
                  @click="getTokenInfo(scope.row.token)"
                >{{ scope.row.token }}</BeeLink>
              </template>
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="登录时间"
              width="160px"
            />
            <el-table-column
              prop="id"
              label="操作"
              width="120px"
            >
              <template slot-scope="scope">
                <BeeButton
                  code="loginUserOffline"
                  size="mini"
                  type="danger"
                  @click="deleteById(scope.row.id)"
                >强制下线
                </BeeButton>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :page-size="size"
            :current-page="current"
            layout="total,sizes, prev, pager, next,jumper"
            :total="total"
            :page-sizes="[ 5, 10, 50, 100]"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
        <el-empty v-else description="查询结果为空" />
      </el-col>
    </el-row>
    <el-dialog
      top="2%"
      :title="dialogInfoTitle"
      :visible.sync="showInfoDialog"
      :close-on-click-modal="false"
      :width="isMobile()?'100%':'50%'"
    >
      <json-viewer
        v-if="tokenInfo!=null"
        style="height: 500px;overflow: auto;"
        :value="tokenInfo"
        :expand-depth="5"
      />
    </el-dialog>
  </div>
</template>
<script>
import BeeButton from '@/components/RoleButton/index.vue'
import { formatData } from '@/utils/ParamFilter'
import { getTokenInfo, page } from '@/api/loginUser'
import { MessageBox } from 'element-ui'
import { deleteById } from '@/api/loginUser'
import BeeLink from '@/components/RoleLink/index.vue'
import { isMobile } from '@/utils/ObjectUtil'

export default {
  name: 'LoginUser',
  components: { BeeLink, BeeButton },
  props: [],
  data() {
    return {
      loading: false,
      infoLoading: false,
      tableData: [],
      current: 0,
      size: 10,
      total: 0,
      multipleSelection: [],
      dialogInfoTitle: '令牌缓存信息',
      showInfoDialog: false,
      tokenInfo: undefined,
      formData: {
        userName: undefined
      },
      rules: {
      }}
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getPageData()
  },
  methods: {
    isMobile,
    getPageData(type) {
      if (type === 'search') {
        this.current = 1
      }
      this.tokenInfo = undefined
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        page(this.formData).then(res => {
          this.tableData = formatData(res.data.records)
          this.total = res.data.total
        }).catch(err => {
          console.log(err)
        }).finally(() => {
          this.loading = false
        })
      })
    },
    deleteById(id) {
      MessageBox({
        title: '提示',
        message: '确定下线?'
      }).then(() => {
        deleteById(id).then(res => {
          if (res.success) {
            this.getPageData()
            this.$message({
              message: '操作成功',
              type: 'success'
            })
          }
        }).catch(err => {
          console.log(err)
        }).finally(() => {
          this.loading = false
        })
      }).catch(() => {
        this.$message({
          message: '取消操作',
          type: 'info'
        })
      })
    },
    handleSizeChange(size) {
      this.size = size
      this.getPageData()
    },
    handleCurrentChange(current) {
      this.current = current
      this.getPageData()
    },
    getTokenInfo(token) {
      this.infoLoading = true
      this.showInfoDialog = true
      const data = {
        token: token
      }
      getTokenInfo(data).then(res => {
        this.tokenInfo = JSON.parse(res.data)
      }).catch(err => {
        console.log(err)
      }).finally(() => {
        this.infoLoading = false
      })
    }

  }
}

</script>
<style>
</style>
